package com.imranstechnology.homeexpensemanager;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.imranstechnology.homeexpensemanager.utils.DateUtil;
import com.imranstechnology.homeexpensemanager.utils.StringUtil;

public class AddExpense extends Activity {
	private Editor prefEditor;
	private Button btnAddExpense;
	private EditText mAddedExpense;
	private Spinner mExpenseCat;
	private EditText mExpenseDate;
	
	private String[] categories = {"Electricity Bill","Water Bill","Mobile Bill","Grossory","Gas Bill","Car Fuel","Vacation","Clothes","Eid Expense","House disposables","Furneture","Other expenses"};
	private DatePickerDialog datePicker;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_expense);
		String currency = PreferenceManager.getDefaultSharedPreferences(this).getString(SettingsActivity.DEFAULT_CURRENCY_KEY, "JOD");		
		SharedPreferences preferences = getSharedPreferences(HomeExpenseConstants.APP_PREPFERENCE_AREA, MODE_PRIVATE);
		prefEditor = preferences.edit();
		// Show the Up button in the action bar.
		setupActionBar();
		btnAddExpense = (Button)findViewById(R.id.btnAddExpense);
		mAddedExpense = (EditText)findViewById(R.id.txtAddedExpense);
		mExpenseCat = (Spinner)findViewById(R.id.acExpenseCat);
		mExpenseDate = (EditText)findViewById(R.id.txtExpenseDateTime);
		TextView curr = (TextView)findViewById(R.id.amountCurr);
		curr.setText(currency);
		categories = getResources().getStringArray(R.array.expense_category);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories);
		mExpenseCat.setAdapter(adapter);
		btnAddExpense.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				addExpense(v);
			}
		});
		final Date curDate = DateUtil.getCurrentDate();
		mExpenseDate.setText(DateUtil.dateToString(curDate));
		datePicker = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
			
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				Calendar cal = Calendar.getInstance();
				cal.set(year, monthOfYear, dayOfMonth);
				mExpenseDate.setText(DateUtil.dateToString(cal.getTime()));
				cal.clear();
			}
			
		},  DateUtil.getCurrentYear(), DateUtil.getCurrentMonth(), DateUtil.getCurrentDay());
		datePicker.setCancelable(true);
		datePicker.setCanceledOnTouchOutside(true);
		
//		datePicker.setOnCancelListener(new DialogInterface.OnCancelListener() {
//			
//			@Override
//			public void onCancel(DialogInterface dialog) {
//				datePicker.cancel();
//			}
//		});
//		datePicker.setOnDismissListener(new DialogInterface.OnDismissListener() {
//			
//			@Override
//			public void onDismiss(DialogInterface dialog) {
//				datePicker.dismiss();
//			}
//		});

	}

	public void showDatePicker(View v){
		datePicker.show();
	}
	protected void addExpense(View v) {
		String value = mAddedExpense.getText().toString();
		String vDate = mExpenseDate.getText().toString();
		String vCat = mExpenseCat.getSelectedItem().toString();
		Integer vCatIndex = mExpenseCat.getSelectedItemPosition();
		
		if(!StringUtil.isEmpty(value) &&
				!StringUtil.isEmpty(vDate) &&
				!StringUtil.isEmpty(vCat))
		{	
			Date dt = DateUtil.stringToDate(vDate);
			if(dt != null)
			{	
				mExpenseDate.setBackgroundColor(Color.WHITE);
				ExpenseMemStore.addExpense(Float.parseFloat(value),dt,vCatIndex);
				ExpenseMemStore.storeMemExpense(prefEditor);
				NavUtils.navigateUpFromSameTask(this);
			}else
			{
				mExpenseDate.setBackgroundColor(Color.RED);
			}
		}
		
	}
	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.options_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.action_settings:
			startActivity(new Intent(this, SettingsActivity.class));
			return true;
		case R.id.expense_detail_view:
			startActivity(new Intent(this, ExpenseDetailView.class));
			return true;
		case R.id.monthly_expense_list_view:
			startActivity(new Intent(this, MonthlyExpenseView.class));
			return true;
		}	
		return super.onOptionsItemSelected(item);
	}
}
